﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Serious
{
    public class GameSprite
    {
        public Texture2D[] Textures;
        public Single AnimationSpeed;
        public Index2 OriginRelative;
        public Rectangle2i BoundsRelative;


        public GameSprite(IEnumerable<Texture2D> textures, Single animationSpeed, Index2 originRelative, Rectangle2i boundsRelative)
        {
            Textures = textures.ToArray<Texture2D>();
            AnimationSpeed = animationSpeed;
            OriginRelative = originRelative;
            BoundsRelative = boundsRelative;
        }
        public GameSprite(IEnumerable<Texture2D> textures, Single animationSpeed, Index2 originRelative)
        {
            Textures = textures.ToArray<Texture2D>();
            AnimationSpeed = animationSpeed;
            OriginRelative = originRelative;
            BoundsRelative = Rectangle2i.ConvertFromXnaRectangle(Textures[0].Bounds);
        }
        public GameSprite(IEnumerable<Texture2D> textures, Single animationSpeed) :
            this(textures, animationSpeed, Index2.Zero)
        {
        }

        public GameSprite(Texture2D tex, Index2 originRelative, Rectangle2i boundsRelative) :
            this(new Texture2D[] { tex }, 0.0f, originRelative, boundsRelative)
        {
        }
        public GameSprite(Texture2D tex, Index2 originRelative) :
            this(new Texture2D[] { tex }, 0.0f, originRelative)
        {
        }
        public GameSprite(Texture2D tex) :
            this(new Texture2D[] { tex }, 0.0f)
        {
        }


        
        public virtual void Render(GameTime gameTime, SpriteBatch spriteBatch, ref Single fuzzyIndex, Single animationDilation, Vector2 location, Vector2 scale, Single angle)
        {
            fuzzyIndex += animationDilation * AnimationSpeed * (Single)gameTime.ElapsedGameTime.TotalSeconds;
            fuzzyIndex %= Textures.Length;
            if (fuzzyIndex < 0) fuzzyIndex += Textures.Length;

            Texture2D tex = Textures[(int)fuzzyIndex];

            SpriteEffects spriteEffect = SpriteEffects.None;
            Index2 originRelative = OriginRelative;
            if (scale.X < 0)
            {
                spriteEffect |= SpriteEffects.FlipHorizontally;
                scale.X = -scale.X;
                originRelative.X = tex.Width - originRelative.X;
            }
            if (scale.Y < 0)
            {
                spriteEffect |= SpriteEffects.FlipVertically;
                scale.Y = -scale.Y;
                originRelative.Y = tex.Height - originRelative.Y;
            }
            spriteBatch.Draw(tex, location, null, Color.White, angle, (Vector2)originRelative, scale, spriteEffect, 0.0f);
        }
        /*
        public void Render(GameTime gameTime, SpriteBatch spriteBatch, ref Single fuzzyIndex, Vector2 location)
        {
            Render(gameTime, spriteBatch, ref fuzzyIndex, location, Vector2.One);
        }
         * */
    }
}
